#include "Iterator.h"
using namespace std;

template<typename T>
class VectorIterator : public iterator<
    random_access_iterator_tag,
    T,
    ptrdiff_t,
    T*,
    T&
> {
private:
    T* current; // 指向动态数组的指针

public:
    // 构造函数
    explicit VectorIterator(T* ptr = nullptr) : current(ptr) {}

    // 解引用操作符
    T& operator*() const { 
        if (!current) throw std::runtime_error("Dereferencing null iterator");
        return *current; 
    }

    T* operator->() const { 
        if (!current) throw std::runtime_error("Dereferencing null iterator");
        return current; 
    }

    // 递增/递减操作符
    VectorIterator& operator++() { 
        current++; 
        return *this; 
    }

    VectorIterator operator++(int) { 
        VectorIterator temp = *this; 
        ++(*this); 
        return temp; 
    }

    VectorIterator& operator--() { 
        current--; 
        return *this; 
    }

    VectorIterator operator--(int) { 
        VectorIterator temp = *this; 
        --(*this); 
        return temp; 
    }

    VectorIterator operator+(ptrdiff_t n) const { 
        return VectorIterator(current + n); 
    }

    VectorIterator& operator+=(ptrdiff_t n) { 
        current += n; 
        return *this; 
    }

    ptrdiff_t operator-(const VectorIterator& other) const { 
        return current - other.current; 
    }

    T& operator[](ptrdiff_t n) const { 
        return *(current + n); 
    }

    // 比较操作符
    bool operator==(const VectorIterator& other) const { 
        return current == other.current; 
    }

    bool operator!=(const VectorIterator& other) const { 
        return !(*this == other); 
    }

    bool operator<(const VectorIterator& other) const { 
        return current < other.current; 
    }
};

template<typename T>
class Vector {
private:
    T* data;         // 动态数组指针
    size_t size_;    // 当前元素数量
    size_t capacity_; // 总容量

    // 内存重新分配辅助函数
    void reallocate(size_t new_capacity) {
        if (new_capacity == 0) {
            delete[] data;
            data = nullptr;
            capacity_ = 0;
            return;
        }

        T* new_data = new T[new_capacity];
        std::copy(data, data + size_, new_data); // 复制原有元素
        delete[] data;
        data = new_data;
        capacity_ = new_capacity;
    }

public:
    // 类型定义
    using value_type = T;
    using reference = T&;
    using const_reference = const T&;
    using iterator = VectorIterator<T>;
    using const_iterator = VectorIterator<const T>;
    using size_type = size_t;

    // 构造/析构
    Vector() : data(nullptr), size_(0), capacity_(0) {}
    explicit Vector(size_t count) : size_(count), capacity_(count) {
        data = new T[count](); // 值初始化
    }
    ~Vector() { delete[] data; }

    // 迭代器接口
    iterator begin() { return iterator(data); }
    iterator end() { return iterator(data + size_); }
    const_iterator begin() const { return const_iterator(data); }
    const_iterator end() const { return const_iterator(data + size_); }

    // 容量接口
    size_type size() const { return size_; }
    size_type capacity() const { return capacity_; }
    bool empty() const { return size_ == 0; }

    // 元素访问
    reference operator[](size_type idx) { 
        if (idx >= size_) throw std::out_of_range("Vector index out of range");
        return data[idx]; 
    }

    const_reference operator[](size_type idx) const { 
        if (idx >= size_) throw std::out_of_range("Vector index out of range");
        return data[idx]; 
    }

    // resize 和 reserve
    void resize(size_type new_size) {
        resize(new_size, T()); // 默认值初始化
    }

    void resize(size_type new_size, const T& value) {
        if (new_size > capacity_) {
            // 容量不足时扩容
            size_t new_capacity = std::max(new_size, capacity_ * 2);
            reallocate(new_capacity);
        }

        // 处理元素增减
        if (new_size > size_) {
            // 填充新元素
            for (size_type i = size_; i < new_size; ++i) {
                data[i] = value;
            }
        } else {
        }
        size_ = new_size;
    }

    void reserve(size_type new_capacity) {
        if (new_capacity > capacity_) {
            reallocate(new_capacity);
        }
    }
};

int main()
{
    Vector<int> vec;
    
    // 添加元素
    vec.resize(5); // 默认初始化为0
    for (size_t i = 0; i < vec.size(); ++i) {
        vec[i] = i * 10;
    }
    
    // 输出元素
    std::cout << "Vector content: ";
    for (auto it = vec.begin(); it != vec.end(); ++it) {
        std::cout << *it << " ";
    }
    std::cout << "\nSize: " << vec.size() 
              << ", Capacity: " << vec.capacity() << std::endl;
    
    // 使用 reserve 预分配空间
    vec.reserve(20);
    std::cout << "After reserve(20): Size=" << vec.size() 
              << ", Capacity=" << vec.capacity() << std::endl;
    
    // 使用带值的 resize
    vec.resize(8, 100); // 扩展并填充100
    std::cout << "After resize(8, 100): ";
    for (const auto& val : vec) { // 使用范围for循环
        std::cout << val << " ";
    }
    std::cout << "\nSize: " << vec.size() 
              << ", Capacity: " << vec.capacity() << std::endl;
    
    // 缩小 size
    vec.resize(3);
    std::cout << "After resize(3): ";
    for (auto it = vec.begin(); it != vec.end(); ++it) {
        std::cout << *it << " ";
    }
    std::cout << "\nSize: " << vec.size() 
              << ", Capacity: " << vec.capacity() << std::endl;
    return 0;
}
